package com.douding.server.service;

import com.douding.server.domain.ChapterExample;
import com.douding.server.domain.Section;
import com.douding.server.domain.SectionExample;
import com.douding.server.domain.Teacher;
import com.douding.server.dto.*;
import com.douding.server.enums.SectionChargeEnum;
import com.douding.server.mapper.SectionMapper;
import com.douding.server.util.CopyUtil;
import com.douding.server.util.UuidUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.List;


import java.util.Date;


@Service
public class SectionService {

    @Resource
    private SectionMapper sectionMapper;

    @Resource
    private CourseService courseService;


    /**
     * 列表查询
     */
    public void list(SectionPageDto sectionPageDto) {
        String chapterId = sectionPageDto.getChapterId();
        String courseId = sectionPageDto.getCourseId();
        SectionExample sectionExample = new SectionExample();
        List<Section> sectionList = sectionMapper.selectByExample(sectionExample);
        List<Section> resList = new ArrayList<>();
        for (Section each : sectionList) {
            if (chapterId.equals(each.getChapterId()) && courseId.equals(each.getCourseId())) {
                resList.add(each);
            }
        }
        sectionPageDto.setList(resList);
    }


    public void save(SectionDto sectionDto) {
        Section section = CopyUtil.copy(sectionDto, Section.class);

        if (StringUtils.isEmpty(section.getId())) { // 判断新增还是修改
            this.insert(section);
        } else {
            this.update(section);
        }
    }

    //新增数据
    private void insert(Section section) {
        section.setId(UuidUtil.getShortUuid());
        sectionMapper.insert(section);

    }

    //更新数据
    private void update(Section section) {
        sectionMapper.updateByPrimaryKey(section);
    }

    public void delete(String id) {
        sectionMapper.deleteByPrimaryKey(id);
    }

    /**
     * 查询某一课程下的所有节
     */
    public List<SectionDto> listByCourse(String courseId) {

        return null;
    }

}//end class
